<!-- 使用 type="home" 属性设置首页，其他页面不需要设置，默认为page；推荐使用json5，更强大，且允许注释 -->
<route lang="json5">
{
  style: {
    navigationStyle: 'custom',
    navigationBarTitleText: '登录',
    navigationBarRightButton: {
      hide: true,
    },
  },
}
</route>
<template>
  <view
    class="w-full h-full bg-white overflow-hidden box-border"
    :style="{ paddingTop: safeAreaInsets?.top + 'px' }"
  >
    <view class="w-full h-full pos-relative z-1 bg-#F6F6F8 box-bg">
      <view class="w-full h-full flex justify-start items-center flex-col">
        <view class="wr-216 hr-216 b-rd-64 bg-white mg-t-210"></view>
        <view class="mg-t-72">
          <text>微信登录并获取您的个人信息需要您的授权</text>
        </view>
        <view class="wr-630 hr-98 mg-t-80">
          <wd-button type="primary" size="small" :loading="loading" @click="login">
            <text class="text-white font-size-32 font-400">确认并同意授权登录</text>
          </wd-button>
        </view>
        <view class="wr-630 hr-98 mg-t-80">
          <wd-button type="info" plain size="small">
            <text class="text-gray-333 font-size-32 font-400">暂不登录</text>
          </wd-button>
        </view>
      </view>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { useUserStore } from '@/store/user'
import { tqWxLogin } from '@/service/login/login'
defineOptions({
  name: 'Login',
})
// 获取屏幕边界到安全区域距离
const loading: Ref<boolean> = ref(false)
const { safeAreaInsets } = uni.getSystemInfoSync()
const userStore = useUserStore()
const login = () => {
  loading.value = true
  uni.getUserInfo({
    provider: 'weixin',
    withCredentials: true,
    success: ({ encryptedData, iv }) => {
      // userInfo, rawData, signature, errMsg
      uni.login({
        provider: 'weixin', // 使用微信登录
        success(lres) {
          const code = lres.code
          tqWxLogin({
            code,
            encryptedData,
            iv,
            loginType: 0,
          }).then((res) => {
            const data: any = res.data
            const userInfo: any = {
              ...data,
              ...{
                code,
                encryptedData,
                iv,
                loginType: 0,
              },
            }
            userStore.setUserInfo(userInfo)
            loading.value = false
            uni.switchTab({
              url: '/pages/home/home',
            })
          })
        },
      })
    },
    fail: (error) => {
      console.log(error)
    },
  })
}
onLoad((options: any) => {})
</script>

<style scoped lang="scss">
.box-bg {
  background: linear-gradient(152deg, #e0e1ff 0%, #fffeff 100%);
}
::v-deep .wd-button {
  width: 100% !important;
  height: 100% !important;
}
</style>
